<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min(1).css">
    <link rel="stylesheet" href="../css/style.css">

    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
    </style>


</head>




<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>角色管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">


                <el-input placeholder="角色名称/角色关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage = 1; findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>


                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>


                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>


            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->

            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">

                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
                                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                                </div>

                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <tbody>
                                        <tr>
                                            <td>选择</td>
                                            <td> 权限名称</td>
                                            <td>关键字</td>
                                            <td>权限说明</td>
                                        </tr>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td style="left: auto"><label>{{c.description}}</label></td>
                                        </tr>
                                        </tbody>

                                    </table>
                                </div>

                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <template>


                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">

                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
                                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                                </div>

                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <tbody>
                                        <tr>
                                            <td>选择</td>
                                            <td> 权限名称</td>
                                            <td>关键字</td>
                                            <td>权限说明</td>
                                        </tr>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td style="left: auto"><label >{{c.description}}</label></td>
                                        </tr>
                                        </tbody>

                                    </table>
                                </div>

                            </el-tab-pane>


                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>

                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>


<script>

    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            permissionIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                code: [{required: true, message: '编码为必填项', trigger: 'blur'}],
                name: [{required: true, message: '名称为必填项', trigger: 'blur'}]
            }
        },

        created() {   //钩子函数 页面加载完毕自动执行
            this.findPage();
        },
        methods: {
            //编辑
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        //表单校验通过，可以提交数据
                        axios.post("/role/edit.do?permissionIds="+this.permissionIds,this.formData).then((res) => {
                            if (res.data.flag) {
                                //弹出成功提示信息
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            } else {
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            //不管成功还是失败，都调用分页查询方法
                            this.findPage();
                            //隐藏编辑窗口
                            this.dialogFormVisible4Edit = false;
                        });
                    } else {
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                });
            },


            //添加
            handleAdd() {
                //权限校验
                this.$refs['dataAddForm'].validate((vali) => {
                    if (vali == true) {
                        //点击确定后，你不管成功还是失败都要关闭添加的窗口
                        this.dialogFormVisible = false;
                        //发送ajax请求
                        axios.post("/role/add.do?permissionIds=" + this.permissionIds, this.formData).then((res) => {
                            //判断后台传递过来的值
                            if (res.data.flag) {
                                //给前端的数据赋值
                                this.formData = res.data.data
                                this.$message.success(res.data.message)
                            } else {
                                this.$message.error(res.data.message)
                            }
                        })/*.catch((req) => {
                this.showMessage(req)   //权限不足的错误
            })*/.finally(() => {
                            //在添加完毕后。无论成功还是失败，都要重新查询数据
                            this.findPage();
                        })
                    } else {
                        this.$message.error("表单校验不通过，请检查后重试")
                    }
                });
            },

            //分页查询   使用了钩子函数  页面加载完毕后自动执行一次
            findPage() {
                //定义数据，查询数据库
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }
                //发送ajax请求
                axios.post("/role/findPage.do", param).then((response) => {
                    //响应回来的数据
                    this.pagination.total = response.data.total    //总条数
                    this.dataList = response.data.rows    //遍历的数据
                })
            },

            // 重置表单
            resetForm() {
                this.formData = {};
            },


            // 弹出添加窗口
            handleCreate() {
                //重置数据
                this.formData = {};
                //重置复选框的值
                this.permissionIds = [];
                //将添加窗口显示出来
                this.dialogFormVisible = true;
                //设置打开添加窗口默认显示基本信息页面
                this.activeName = 'first';
                //发送ajax请求，不需要传递数据，直接查询所有的检查项数据   用于勾选
                axios.get("/permission/findAll.do").then((resoponse) => {
                    if (resoponse.data.flag == true) {
                        //返回的状态为 true  ，将返回的数据复制给 formData 用于显示
                        this.tableData = resoponse.data.data;
                    } else {
                        //返回状态为false 表示没查到或查询中出错
                        this.$message.error(resoponse.data.message);
                    }
                }).catch((response) => {
                    this.showMessage(response)
                });
            },


            //弹出编辑窗口
            handleUpdate(row) {   //这个row有这一行的所有的数据
                this.formData = {};
                this.activeName = 'first';
                this.permissionIds = [];
                this.dialogFormVisible4Edit = true;
                //查询数据库，查询出编辑的这个权限的详细信息用于展示
                axios.get("/role/findById.do?id=" + row.id).then((res) => {
                    //判断状态
                    if (res.data.flag) {
                        //给窗口赋值
                        this.formData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                //发送ajax请求，不需要传递数据，直接查询所有的检查项数据   用于勾选
                axios.get("/permission/findAll.do").then((resoponse) => {
                    if (resoponse.data.flag == true) {
                        //返回的状态为 true  ，将返回的数据复制给 formData 用于显示
                        this.tableData = resoponse.data.data;
                        axios.get("/role/findByRoleIdsByPermissionId.do?id=" +row.id).then((res)=>{
                            if (res.data.data){
                                this.permissionIds=res.data.data;
                                //this.formData=res.data.data;
                            } else {
                                this.$message.error(resoponse.data.message);
                            }
                        });
                    } else {
                        //返回状态为false 表示没查到或查询中出错
                        this.$message.error(resoponse.data.message);
                    }
                }).catch((response) => {
                    this.showMessage(response)
                });
            },


            //切换页码  参数 currentPage 是切换后的页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            //权限不足框架抛出的异常信息
            showMessage(r) {
                if (r == "Error: Request failed with status code 403") {
                    //权限不足 ? ?
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },


            // 删除
            handleDelete(row) {
                //删除属于危险操作，必须提示
                this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post("/role/delete.do?id=" + row.id).then((response) => {
                        if (response.data.flag) {
                            this.$message.success(response.data.message)
                        } else {
                            this.$message.error(response.data.message)
                        }
                    }).catch((r) => {
                        this.showMessage(r)   //权限不足的提示
                    }).finally(() => {
                        this.findPage();
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }

    })

</script>
</html>
